var candidates = null;
var	candidates_per_page = 14;
var selected_candidates = {};
var activity_id = 0; 
$(function(){
    $('#invite-dlg').dialog({
		autoOpen: false,
		modal: true,
		width : 450,
		height: 350,
	});	
	$('#invite').click(function(){
		if (candidates == null) {
	    	$.get('/activity/candidates/' + activity_id,function(data){
	    		candidates = data.candidates;
	    		for (var i=0; i < candidates.length; i++) {
	    			selected_candidates[candidates[i].id] = false;
				};
				 
				$('#invite-dlg-candidates').pagination(candidates.length, {
					items_per_page : candidates_per_page,
					load_first_page: true,
					next_text: '->',
					next_show_always: false,
					prev_text: '<-',
					prev_show_always: false,					
					callback : function(index, container){	
						$('#invite-dlg-candidates').prepend('<div class="clear"></div>');			
						$('#invite-dlg-candidates').prepend('<div class="float-left"></div>');		
						$container = $('#invite-dlg-candidates').children().first()
						for (var i = candidates_per_page * index; i < Math.min(candidates_per_page * (index + 1), candidates.length); i++) {
							html = '<div class="user_candidate float-left ';
							if (selected_candidates[candidates[i].id] == true){
								html += 'ui-state-active';
							}
							html += '" id="' + candidates[i].id + '"><img src="' + candidates[i].portrait + '"/><div>' + candidates[i].name + '</div></div>';
							$container.prepend(html);
						};
						$('.user_candidate').click(function(){
							$(this).toggleClass('ui-state-active');
							selected_candidates[$(this).attr('id')] = !selected_candidates[$(this).attr('id')];
						});							
					},
				});
	    	})
		};
		$('#invite-dlg').dialog('open');	
		return false;    	
	});
	    
	    

	    
})
var get_candidates_str = function(){
	var candidates_str = '';
	for(var key in selected_candidates){ 
		if (selected_candidates[key]) {
			candidates_str += key;
			candidates_str += '&'
		};
	}
	candidates_str = candidates_str.slice(0, -1);
	$('#invite-list').attr('value', candidates_str);
};

